package bubble;

import java.awt.*;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.Window.Type;
import java.awt.*;

public class APIW {

	private JFrame frame;
	static int NUMBER;
	static private JSpinner spinner;
	static public JTextPane[] textPane2;
	static private JLabel lblSizeOfArray;
	static private JButton btnNewButton_1;
	static private JComboBox comboBox;
	static private JLabel lblMethodOfSort;
	static public int[] array;
	public static JTextPane[] textPane1;
	private JPanel panel_1;
	static private JPanel panel;
	static boolean FLAG = true;
	static Thread t;

	public APIW() {
		initialize();
	}

	private void initialize() {
		setFrame(new JFrame());
		getFrame().setResizable(false);
		getFrame().setBounds(100, 100, 576, 355);
		getFrame().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JButton btnNewButton = new JButton("Start");
		btnNewButton.setBackground(new Color(173, 216, 230));
		btnNewButton.setBounds(69, 231, 139, 40);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (FLAG == true) {
					FLAG = false;
					Runnable r = new NewThread();
					t = new Thread(r);
					t.start();

				}
			}
		});
		getFrame().getContentPane().setLayout(null);
		getFrame().getContentPane().add(btnNewButton);
		lblSizeOfArray = new JLabel(
				"Size of array:");
		lblSizeOfArray.setFont(new Font("Century Gothic", Font.PLAIN, 14));
		lblSizeOfArray.setBounds(10, 11, 172, 17);
		getFrame().getContentPane().add(lblSizeOfArray);
		btnNewButton_1 = new JButton("OK");
		btnNewButton_1.setBackground(new Color(173, 216, 230));
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				array = new int[NUMBER];
				try {
					switch (comboBox.getSelectedIndex()) {
					case 0:
						bubble.Generation.randomGen(array, 1, 40);
						break;

					case 1:
						bubble.Generation.orderedGen(array);
						break;

					case 2:
						bubble.Generation.reverseGen(array);
						break;
					default:
						bubble.Generation.randomGen(array, 1, 40);
						break;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				if (FLAG == true)
					PrintPanes(array);
			}
		});

		btnNewButton_1.setBounds(383, 53, 100, 24);
		getFrame().getContentPane().add(btnNewButton_1);

		spinner = new JSpinner();
		spinner.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				NUMBER = (int) spinner.getValue();
				if (NUMBER > 20)
					NUMBER = 20;
			}
		});
		spinner.setBounds(179, 9, 49, 20);
		getFrame().getContentPane().add(spinner);

		lblMethodOfSort = new JLabel(
				"Method of sort:");
		lblMethodOfSort.setFont(new Font("Century Gothic", Font.PLAIN, 14));
		lblMethodOfSort.setBounds(10, 55, 139, 22);
		getFrame().getContentPane().add(lblMethodOfSort);

		comboBox = new JComboBox();
		comboBox.setFont(new Font("Centaur", Font.PLAIN, 15));
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				array = new int[NUMBER];
				try {
					switch (comboBox.getSelectedIndex()) {
					case 0:
						bubble.Generation.randomGen(array, 1, 40);
						break;

					case 1:
						bubble.Generation.orderedGen(array);
						break;

					case 2:
						bubble.Generation.reverseGen(array);
						break;

					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"Random", "Order", "Reverse"}));
		comboBox.setBounds(159, 54, 203, 24);
		getFrame().getContentPane().add(comboBox);

		JButton btnNewButton_2 = new JButton("Stop");
		btnNewButton_2.setBackground(new Color(173, 216, 230));
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				t.stop();
				FLAG = true;
			}
		});
		btnNewButton_2.setBounds(344, 231, 139, 40);
		frame.getContentPane().add(btnNewButton_2);

	}

	public void PrintPanes(int arr[]) {
		JPanel panel = new JPanel();
		getFrame().getContentPane().add(panel);
		panel.setBounds(10, 135, 640, 23);
		panel.setLayout(null);
		textPane2 = new JTextPane[NUMBER];
		Color c = new Color(30, 144, 255);
		for (int i = 0; i < arr.length; i++) {

			textPane2[i] = new JTextPane();
			textPane2[i].setEditable(false);
			textPane2[i].setForeground(Color.BLACK);
			String str2 = arr[i] + " ";
			textPane2[i].setText(str2);
			textPane2[i].setBackground(c);
			textPane2[i].setBounds(i * 28, 0, 22, 40);
			panel.add(textPane2[i]);
		}
	}

	public JFrame getFrame() {
		return frame;
	}

	public void setFrame(JFrame frame) {
		this.frame = frame;
		frame.setFont(new Font("Segoe Script", Font.PLAIN, 14));
		frame.setTitle("\u0421\u043E\u0440\u0442\u0438\u0440\u043E\u0432\u043A\u0430 \u043F\u0443\u0437\u044B\u0440\u044C\u043A\u043E\u043C");
	}
}